package cn.com.oceansoft.modules.tasks.controller;

import cn.com.oceansoft.application.constant.ConfigInfo;
import cn.com.oceansoft.application.common.util.HttpClientUtil;
import cn.com.oceansoft.framework.kits.ApiResult;
import com.alibaba.fastjson.JSONObject;
import nl.captcha.Captcha;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * Created by zhangxh on 2016/10/3.
 * 办件进度查询
 */
@Controller
@RequestMapping("taskbases")
public class TaskBaseController {
    @Resource
    protected ConfigInfo configInfo;
    @RequestMapping("to_search")
    public String toSearch(){
        return "tasks/to_search";
    }
    @ResponseBody
    @RequestMapping("validatorCode")
    public ApiResult validateCode(HttpServletRequest request){
        String myCode=request.getParameter("my_code");
        if(StringUtils.isEmpty(myCode)){
            return new ApiResult(false, 10400, "验证码不能为空", null);
        }
        Captcha captcha= (Captcha) request.getSession().getAttribute(Captcha.NAME);
        if(!captcha.isCorrect(myCode)){
            return new ApiResult(false, 10400, "验证码错误", null);
        }
        return new ApiResult(true,10200,"",null);
    }
    @ResponseBody
    @RequestMapping("num")
    public ApiResult num(HttpServletRequest request){
        ApiResult apiResult=null;
            String num=request.getParameter("num");
            String result = HttpClientUtil.doGet(configInfo.getMsHostUrl()+"tasks/num/"+num);
            apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }

    @ResponseBody
    @RequestMapping(value = "page")
    public ApiResult page(@RequestBody Map<String,String> map, HttpServletRequest request){
        ApiResult apiResult=null;
            String result = HttpClientUtil.doGet(configInfo.getMsHostUrl()+"tasks/page",map);
            apiResult = JSONObject.parseObject(result, ApiResult.class);
        return apiResult;
    }
}
